/*
   @Copyright:LintCode
   @Author:   tjyemail
   @Problem:  http://www.lintcode.com/problem/remove-duplicates-from-sorted-array-ii
   @Language: C++
   @Datetime: 16-02-09 05:15
   */

class Solution {
public:
	/**
	 * @param A: a list of integers
	 * @return : return an integer
	 * Tips: Using two iterator left and right
	 *       Using c to record the num appear times
	 */
	int removeDuplicates(vector<int> &A) {
		// write your code here
		if (A.size() < 1) return 0;
		int c = 1;		// count current num appear times
		vector<int>::iterator l, r;
		for(r=A.begin(), l=r++; r!=A.end(); ++r){
			if (*r != *l){
				*(++l) = *r;
				c = 1;
			}
			else if(c == 1){
				*(++l) = *r;
				++c;
			}
		}
		A.erase(++l, r);
		return A.size();
	}
};
